//
//  MainTabviewController.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/4/13.
//

import UIKit

class MainTabviewController: CommonViewController {
    private let table = UITableView.init(frame: CGRect.zero, style: .plain)
    private let tip = UILabel()
    private var dataArray:Array<NewsModel> = []
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.addSubview(table)
        table.snp.makeConstraints({
            $0.edges.equalToSuperview()
        })
        table.delegate = self
        table.dataSource = self
        table.estimatedSectionHeaderHeight = 0
        table.estimatedSectionFooterHeight = 0
        table.estimatedRowHeight = 44.0
        table.rowHeight = UITableView.automaticDimension
        view.addSubview(tip)
        table.tableFooterView = UIView()
        table.register(TextNewsCell.self, forCellReuseIdentifier: "TextNewsCell")
        table.register(BigImageNewsCell.self, forCellReuseIdentifier: "BigImageNewsCell")
        table.register(ImagesNewsCell.self, forCellReuseIdentifier: "ImagesNewsCell")
        table.register(ImageNewsCell.self, forCellReuseIdentifier: "ImageNewsCell")
        table.separatorColor = Separator_COLOR
        table.separatorInset = UIEdgeInsets.init(top: 0, left: 15, bottom: 0, right: 15)
        
        for _ in 0...2 {
            var item = NewsModel()
            item.title = "加快建设体育强国"
            item.name = "新华网客户端"
            item.isTop = true
            dataArray.append(item)
        }
        for _ in 0...2 {
            var item = NewsModel()
            item.title = "加快建设体育强国,加快建设体育强国"
            item.name = "人民网"
            item.isSubject = true
            item.type = .BigImage
            dataArray.append(item)
        }
        for _ in 0...1 {
            var item = NewsModel()
            item.title = "加快建设体育强国,加快建设体育强国"
            item.name = "人民网"
            item.isSubject = true
            item.type = .Images
            dataArray.append(item)
        }
        for _ in 0...2 {
            var item = NewsModel()
            item.title = "加快建设体育强国,加快建设体育强国设体育强国,加快建设体育强设体育强国"
            item.name = "新浪网"
            item.type = .NormalImage
            dataArray.append(item)
        }
       
    }
    func reloadData(text:String) {
        tip.text = text
        
    }
    
}
extension MainTabviewController:UITableViewDelegate{
//    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
////        self.pushViewController(FansViewController(), animated: true)
//    }


}

extension MainTabviewController:UITableViewDataSource{
   
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let item = dataArray[indexPath.row]
        switch item.type {
        case .Text:
            let cell = table.dequeueReusableCell(withIdentifier: "TextNewsCell") as! TextNewsCell
            cell.bind(item: item)
            return cell
        case .NormalImage:
            let cell = table.dequeueReusableCell(withIdentifier: "ImageNewsCell") as! ImageNewsCell
            cell.bind(item: item)
            return cell
        case .BigImage:
            let cell = table.dequeueReusableCell(withIdentifier: "BigImageNewsCell") as! BigImageNewsCell
            cell.bind(item: item)
            return cell
        case .Images:
            let cell = table.dequeueReusableCell(withIdentifier: "ImagesNewsCell") as! ImagesNewsCell
            cell.bind(item: item)
            return cell
        default: 
            let cell = table.dequeueReusableCell(withIdentifier: "TextNewsCell") as! TextNewsCell
            cell.bind(item: item)
            return cell
        }
    } 
}
